
package com.jf.cloud.flow.controller.multishop;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.i18n.I18nMessage;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.ExcelUtil;
import com.jf.cloud.flow.dto.FormDTO;
import com.jf.cloud.flow.model.Form;
import com.jf.cloud.flow.service.FormService;
import com.jf.cloud.api.order.vo.FormExcelParamVO;
import com.jf.cloud.flow.vo.FormExcelVO;
import com.jf.cloud.flow.vo.FormItemVO;
import com.jf.cloud.flow.vo.FormVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 数据表格
 * @author zz
 */
@RestController("multishopFormController")
@RequestMapping("/m/form")
@Tag(name = "数据表格")
public class FormController {

    @Autowired
    private FormService formService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取数据表格列表", description = "分页获取数据表格列表")
    public ServerResponseEntity<PageVO<Form>> page(@Valid PageDTO pageDTO, @ParameterObject FormDTO formParam) {
        Long shopId = AuthUserContext.get().getTenantId();
        formParam.setShopId(shopId);
        PageVO<Form> formPage = formService.page(pageDTO, formParam);
        return ServerResponseEntity.success(formPage);
    }

    @GetMapping
    @Operation(summary = "获取数据表格", description = "根据formId获取数据表格")
    public ServerResponseEntity<Form> getByFormId(@RequestParam Long formId) {
        Form form = formService.getByFormId(formId);
        if (Objects.isNull(form)) {
            throw new LuckException("当前报表不存在，请刷新页面重试");
        }
        return ServerResponseEntity.success(form);
    }

    @PostMapping
    @Operation(summary = "保存数据表格", description = "保存数据表格")
    public ServerResponseEntity<Void> save(@Valid @RequestBody FormDTO formDTO) {
        Form form = mapperFacade.map(formDTO, Form.class);
        form.setShopId(AuthUserContext.get().getTenantId());
        form.setFormId(null);
        formService.save(form);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新数据表格", description = "更新数据表格")
    public ServerResponseEntity<Void> update(@Valid @RequestBody FormDTO formDTO) {
        Form form = mapperFacade.map(formDTO, Form.class);
        Long shopId = AuthUserContext.get().getTenantId();
        Form byFormId = formService.getByFormId(form.getFormId());
        if (!Objects.equals(byFormId.getShopId(),shopId)) {
            throw new LuckException("您没有权限更改该报表的数据");
        }
        formService.update(form);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除数据表格", description = "根据数据表格id删除数据表格")
    public ServerResponseEntity<Void> delete(@RequestParam Long formId) {
        formService.deleteById(formId);
        return ServerResponseEntity.success();
    }

    @GetMapping("/get_form_item")
    @Operation(summary = "获取报表项列表" , description = "获取报表项列表")
    public ServerResponseEntity<List<FormItemVO>> getFormItem() {
        List<FormItemVO> formItemEnumList = formService.getFormItem(2, I18nMessage.getLang());
        return ServerResponseEntity.success(formItemEnumList);
    }

    @GetMapping("/form_excel")
    @Operation(summary = "根据报表id生成对应的报表统计数据" , description = "根据报表id生成对应的报表统计数据")
    public void formExcel(@RequestParam("formId") Long formId, HttpServletResponse response) {
        Form form = formService.getByFormId(formId);
        if (Objects.isNull(form)) {
            throw new LuckException("报表不存在");
        }
        FormExcelParamVO excelParam = formService.getExcelParam(form);
        List<FormExcelVO> list = formService.listExcel(form, excelParam);
        ExcelUtil.soleExcel(response, list, form.getFormName(), FormExcelVO.MERGE_ROW_INDEX, FormExcelVO.MERGE_COLUMN_INDEX, excelParam.getFormItemNames(), FormExcelVO.class);
    }

    @GetMapping("/get_recommend_form_list")
    @Operation(summary = "获取全部推荐报表" , description = "获取全部推荐报表")
    public ServerResponseEntity<List<FormVO>> getRecommendFormList() {
        List<FormVO> formList = formService.getRecommendFormList();
        return ServerResponseEntity.success(formList);
    }
}
